package davion.study.summercloud.util;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author Davion
 * @date 2021/03/08
 */
public enum FastDFSUtil {
    /**
     * 枚举单例
     */
    INSTANCE;

    private final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);

    TrackerClient tracker = null;

    TrackerServer trackerServer = null;

    StorageServer storageServer = null;

    StorageClient1 client = null;

    FastDFSUtil() {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
            tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            client = new StorageClient1(trackerServer, storageServer);
            logger.info("fastdfs系统初始化完成");
        } catch (Exception e) {
            logger.error("fastdfs系统初始化失败");
        }
    }

    public String uploadFile(byte[] buffer, String suffix) throws Exception {
        return client.upload_file1(buffer, suffix, null);
    }

    public byte[] downloadFile(String fileId) throws IOException, MyException {
        return client.download_file1(fileId);
    }

    public int deleteFile(String fileId) throws IOException, MyException {
        return client.delete_file1(fileId);
    }
}
